#include "LinkList.h"
#include <iostream>
int LinkList::ListCount = 0;

LinkList::LinkList(void)
{
	count = 0;
	dataLinkHead = dataLinkTail = nullptr;
	++ListCount;
}

LinkList::~LinkList(void)
{
	while (dataLinkHead)
	{
		ListData nextNode = dataLinkHead->next;
		delete dataLinkHead;
		dataLinkHead = nextNode;
	}
	--ListCount;
}

void LinkList::putTail(int newData) throw(std::bad_alloc)
{
	if (dataLinkHead == nullptr)
	{
		dataLinkHead = dataLinkTail = new ListDataNode(newData,nullptr);
		++count;
		return;
	}
	dataLinkTail->next = new ListDataNode(newData, nullptr);
	dataLinkTail = dataLinkTail->next;
	++count;
}

void LinkList::putHead(int newData) throw(std::bad_alloc)
{
	if (dataLinkHead == nullptr)
	{
		dataLinkHead = dataLinkTail = new ListDataNode(newData,nullptr);
		++count;
		return;
	}
	ListData newNode = new ListDataNode(newData,dataLinkHead);
	dataLinkHead = newNode;
	++count;
}

int LinkList::getHead(void)
{
	int data = dataLinkHead->data;
	ListData newHead = dataLinkHead->next;
	delete dataLinkHead;
	--count;
	dataLinkHead = newHead;
	return data;
}

int LinkList::peekHead(void)
{
	return dataLinkHead->data;
}

bool LinkList::empty()
{
	return dataLinkHead == nullptr;
}

int LinkList::getElemCount()
{
	return count;
}

int LinkList::getListNumber()
{
	return ListCount;
}

void LinkList::print()
{
	/*LinkHead-->0-->1-->2-->3 */
	using std::cout;
	using std::endl;
	cout << "LinkHead";
	auto point = dataLinkHead;
	while(point)
	{
		cout << "-->" << point->data;
		point = point->next;
	}
}
